//
//  ReadingViewController.swift
//  TravelNotes
//
//  Created by 千锋 on 16/8/15.
//  Copyright © 2016年 胡光健. All rights reserved.
//

import UIKit

class ReadingViewController: RootViewController, UIScrollViewDelegate {

    var segment = UISegmentedControl()
    var scrollView = UIScrollView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

       createNav()
        createUI()
    }
    
    func createUI() {
        
        scrollView = UIScrollView(frame: CGRectMake(0,30,screenWidth,screenHeight))
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        self.view.addSubview(scrollView)
        scrollView.contentSize = CGSizeMake(2 * screenWidth, 0)
        
        let travelNotesVC = TravelNotesViewController()
        travelNotesVC.view.backgroundColor = UIColor.whiteColor()
        
        let specialTopicVC = SpecialTopicViewController()
        specialTopicVC.view.backgroundColor = UIColor.whiteColor()
        
        let array = [travelNotesVC, specialTopicVC]
        
        for i in 0..<array.count {
            let vc = array[i]
            
            vc.view.frame = CGRectMake(CGFloat(i) * screenWidth, 0, screenWidth, screenHeight)
            self.addChildViewController(vc)
            self.scrollView.addSubview(vc.view)
        }
        
    }
    func createNav() {
        
        self.titleLabel?.text = "兮札记"
        segment = UISegmentedControl(frame: CGRectMake(10,0,screenWidth - 20,30))
        segment.insertSegmentWithTitle("游记", atIndex: 0, animated: true)
        segment.insertSegmentWithTitle("专题", atIndex: 1, animated: true)
        segment.backgroundColor = UIColor.whiteColor()
        self.segment.tintColor = UIColor.RGBA(85, 164, 154)
        self.view.addSubview(segment)
        segment.addTarget(self, action: "valueChanged:", forControlEvents: .ValueChanged)
        segment.selectedSegmentIndex = 0
        
    }
    func valueChanged(segment: UISegmentedControl) {
        scrollView.contentOffset = CGPointMake(CGFloat(segment.selectedSegmentIndex) * screenWidth, 0)
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = scrollView.contentOffset.x / screenWidth
        
        self.segment.selectedSegmentIndex = Int(index)
    }
}
